﻿using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using com.fly.sms;

namespace com.fly.sms
{
    public class SendTaskAction:BaseAction
    {
        public SendTaskAction(HttpContext contextTemp)
        {
            this.context = contextTemp;
        }
        public override void excute()
        {
            string json = "";
            try
            {
                string Name = context.Request["Name"];
                string MsgContent = context.Request["MsgContent"];
                string DestNumber = context.Request["DestNumber"];

                string FromDate = context.Request["FromDate"];
                string ToDate = context.Request["ToDate"];

                string TaskType = context.Request["TaskType"];

                string Condition = context.Request["Condition"];

                string StartTime = context.Request["StartTime"];


                int msgLength = MsgContent.Length;
                int msgCount = 1;
                if (msgLength > 70)
                {
                    if (msgLength % 65 == 0)
                    {
                        msgCount = msgLength / 65;
                    }
                    else
                    {
                        msgCount = msgLength / 65 + 1;
                    }
                }
                string[] DestNumberArray = DestNumber.Split(new Char[] { ';' });

                //添加待发送短信
                bool rst = SMTaskService.add(guid, guid, CustomerId, Name, MsgContent, msgCount * DestNumberArray.Length, DestNumberArray.Length, TaskType, FromDate, ToDate, Condition, StartTime);
                //或缺添加后的Id
                //string SMTaskId = SMTaskService.getId(Name);
                foreach (string mobile in DestNumberArray)
                {
                    SMTaskDetailService.add(guid, mobile, msgCount);
                }
                ht.Add("responseCode", "200");
                ht.Add("responseData", rst);
                json = JsonConvert.SerializeObject(ht);
                context.Response.Write(json);  
            }
            catch (Exception e)
            {
                ht.Add("responseData", e.Message);
                ht.Add("responseCode", "500");
                json = JsonConvert.SerializeObject(ht);
                context.Response.Write(json);
            }
                     
        }
    }
}